package com.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.Medicine;
import com.demo.entity.MedicineCate;
import com.demo.entity.Vo.MedicineVo;
import com.demo.mapper.MedicineCateMapper;
import com.demo.mapper.MedicineMapper;
import com.demo.response.Result;
import com.demo.service.IMedicineCateService;
import com.demo.service.IMedicineService;
import com.demo.service.IUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预防药物表 服务实现类
 *
 * @author ZM
 * @since 2020-11-20
 */
@Service
@Slf4j
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements IMedicineService {
    @Resource
    MedicineMapper medicineMapper;
    @Autowired
    IUserService userService;
    @Autowired
    IMedicineCateService cateService;
    @Resource
    MedicineCateMapper cateMapper;


    /**
     * 新增预防药物表
     *
     * @param medicine 实体对象
     * @return
     */
    @Override
    public Result addMedicine(Medicine medicine) {
        boolean flag = save(medicine);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 更新预防药物表
     *
     * @param medicine 实体对象
     * @return
     */
    @Override
    public Result updateMedicine(Medicine medicine) {
        if (medicine.getMedicineId() == null || medicine.getUpdatedUserId() == null) {
            return Result.error(Constant.KEY_ID);
        }
        medicine.setUpdatedTime(new Date());
        boolean flag = updateById(medicine);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 删除预防药物表
     *
     * @param deletedUserId 删除人ID
     * @param medicineId    预防药物ID
     * @return
     */
    @Override
    public Result deleteMedicine(Integer medicineId, Integer deletedUserId) {
        Medicine medicine = new Medicine();
        medicine.setMedicineId(medicineId);
        medicine.setDeletedUserId(deletedUserId);
        medicine.setDeletedTime(new Date());
        boolean flag = updateById(medicine);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 预防药物表列表
     *
     * @param pageNumber   页码
     * @param pageSize     每页条数
     * @param medicineId   预防药物ID
     * @param medicineName 药物名称
     * @return
     */
    @Override
    public Result medicineList(Integer pageNumber, Integer pageSize, Integer medicineId, String medicineName, Boolean allItem) {
        IPage<Medicine> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(medicineId != null, "medicine_id", medicineId);
        queryWrapper.like(medicineName != null && !"".equals(medicineName), "medicine_name", medicineName);
        if (!allItem) {
            queryWrapper.isNull("deleted_user_id");
        }
        queryWrapper.orderByAsc("medicine_cate_id");
        queryWrapper.orderByAsc("item_sort");
        page = medicineMapper.selectPage(page, queryWrapper);
        List<Medicine> list = page.getRecords();

        List<MedicineVo> medicineVoList = new ArrayList<>();
        for (Medicine medicine : list) {
            MedicineVo medicineVo = new MedicineVo();
            BeanUtils.copyProperties(medicine, medicineVo);
            medicineVo.setCreatdUserName(userService.getUnameById(medicine.getCreatedUserId()));
            medicineVo.setUpdateUserName(userService.getUnameById(medicine.getUpdatedUserId()));
            MedicineCate medicineCate = cateMapper.selectById(medicine.getMedicineCateId());
            if (medicineCate != null) {
                medicineVo.setProposal(medicineCate.getProposal());
                medicineVo.setCategory(medicineCate.getCategory());
            }
            medicineVoList.add(medicineVo);
        }
        PageInfo<MedicineVo> pageInfo = new PageInfo<>(medicineVoList);
        pageInfo.setPages((int) page.getPages());
        pageInfo.setTotal(page.getTotal());
        if (pageNumber.equals(page.getPages())) {
            pageInfo.setIsLastPage(false);
        } else {
            pageInfo.setIsLastPage(true);
        }
        return Result.success(pageInfo);
    }

    /**
     * 查询某一类下是否存在药物
     *
     * @param cateId 类目ID
     * @return
     */
    @Override
    public boolean getAll(Integer cateId) {
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("deleted_user_id");
        queryWrapper.eq("medicine_cate_id", cateId);
        queryWrapper.select("medicine_id");
        IPage<Medicine> page = new Page<>(1, 1);
        page = medicineMapper.selectPage(page, queryWrapper);
        if (page.getTotal() > 0) {
            return false;
        }
        return true;
    }


}
